import pygame
from pygame.sprite import Sprite


class Enemy(Sprite):
    """表示敌人的类"""

    def __init__(self, gm):
        """初始化敌人并设置起始位置"""
        super().__init__()
        self.screen = gm.screen
        self.settings = gm.settings

        # 加载敌人
        self.image = pygame.image.load("./images/enemy1.png")
        self.rect = self.image.get_rect()
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height
        # 存储用浮点数表示的敌人水平位置
        self.x = float(self.rect.x)

    def check_edges(self):
        """检查敌人是否位于屏幕边缘"""
        screen_rect = self.screen.get_rect()
        return (self.rect.right >= screen_rect.right) or (self.rect.left <= 0)

    def update(self):
        """向左或向右移动敌人"""
        self.x += self.settings.enemy_speed * self.settings.fleet_direction
        self.rect.x = self.x
